<?php
/**
 * auth: WQl
 * Class Req
 */

class Req
{
    //当前请求参数
    public $params = [];

    //用户ip
    public $_from_ip;

    //请求头
    public $header;

    //请求方法
    public $method;

    //请求url
    public $uri;

    //请求路由
    public $path;

    //本次请求编号
    public $request_no;

    //请求时间
    public $request_time;

    //上传的文件
    public $file;

    //后台用户id
    public $user_id;

    //后台用户信息
    public $userInfo;

    //前台用户的会员id
    public $memberId;

    //前台用户的会员信息
    public $memberInfo;

    //操作成功默认返回
    public function success($data = [], $code = 200, $msg = "操作成功"): \Workerman\Protocols\Http\Response
    {
        return new \Workerman\Protocols\Http\Response(200,
            [
                'Content-Type'=>'application/json',
//                'Access-Control-Allow-Origin'=>'*',                               //cors 跨域开启
//                'Access-Control-Allow-Methods'=>'GET, POST, PUT, OPTIONS',        //cors 跨域开启
//                'Access-Control-Allow-Headers'=>'*',                              //cors 跨域开启
//                'Access-Control-Expose-Headers'=>'Content-Length,Content-Range'   //cors 跨域开启
            ],
            json_encode(['code' => $code, 'data' => $data, 'msg' => $msg],JSON_UNESCAPED_UNICODE)
        );
    }
    //操作失败默认返回
    public function no($msg = "操作失败", $code = 0 ,$data = []): \Workerman\Protocols\Http\Response
    {
        return new \Workerman\Protocols\Http\Response(200,
            [
                'Content-Type'=>'application/json',
//                'Access-Control-Allow-Origin'=>'*',                               //cors 跨域开启
//                'Access-Control-Allow-Methods'=>'GET, POST, PUT, OPTIONS',        //cors 跨域开启
//                'Access-Control-Allow-Headers'=>'*',                              //cors 跨域开启
//                'Access-Control-Expose-Headers'=>'Content-Length,Content-Range'   //cors 跨域开启
                ],
            json_encode(['code' => $code, 'data' => $data, 'msg' => $msg],JSON_UNESCAPED_UNICODE)
        );
    }

    //请求参数
    public function param($name = "",$default = null){
        return $name ? $this->params[$name] ?? $default : $this->params;
    }

    //获取后台用户id
    public function getUserId()
    {
        return $this->user_id;
    }

    //检查手机号是否合法  true 合法 false 不合法
    public function checkPhone($phone): bool
    {
        preg_match_all("/^1[34578]\d{9}$/", $phone, $mobiles);
        return isset($mobiles[0]) && isset($mobiles[0][0]);
    }

    /**
     * 检测微信号
     * auth WQl
     * 2021/6/22 14:19
     * @param $weChat
     * @return bool true合法 false不合法
     */
    public function checkWechat($weChat): bool
    {
        preg_match_all("/^[a-zA-Z][a-zA-Z\d_-]{5,19}$/",$weChat,$weChats);
        return isset($weChats[0]) && isset($weChats[0][0]);
    }

    /**
     * 简单检查数组参数及返回提示
     * auth WQl
     * 2021/6/25 10:51
     * @param $arr
     * @param $checkArr
     * @return false|mixed
     */
    public function isSetCheck($arr,$checkArr)
    {
        foreach ($checkArr as $k => $v)
        {
            if(!isset($arr[$k])) return $v;
        }
        return false;
    }

    //生成一个流水id
    public function getTradeId() :string
    {
        $tradeId = date('Ymd').rand(100000, 999999).substr(microtime(true),-4);
        return str_replace('.','0',$tradeId);
    }

    //前台用户使用，获取用户当前平台
    public function getPlatform()
    {
        return $this->header['platform'] ?? 0;
    }

    //前台用户使用，获取当前登录的会员id
    public function getMemberId()
    {
        return $this->memberId;
    }

    //身份证号规则验证
    public function checkIdNo($idNo):bool
    {
        return in_array(strlen($idNo),[15,18]);
    }

    //发起post 请求
    function curl($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 300);
        // 最大执行时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output ;
    }

    //毫秒级时间戳
    public function msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}